package com.thudojsc.canvasengine;

import com.thudojsc.canvasengine.entity.Scene;
import com.thudojsc.canvasengine.entity.Sprite;
import com.thudojsc.canvasengine.entity.Text;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceView;
import android.view.WindowManager;


public class CanvasEngineActivity extends Activity {
	public static Bitmap mBitmapRed;
	public static Bitmap mBitmapBlack;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        final DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        
        
        final SurfaceView surfaceView = new SurfaceView(this);
        
        setContentView(surfaceView);
        
        final Engine engine = new Engine();
        
        surfaceView.getHolder().addCallback(engine);
        surfaceView.setOnTouchListener(engine);
        
        mBitmapRed = BitmapFactory.decodeResource(getResources(), R.drawable.card_43);
        mBitmapBlack = BitmapFactory.decodeResource(getResources(), R.drawable.card_00);
        /*
        setContentView(R.layout.main);
        
        MainScene.BACKGROUND_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card_43);
        PlayerAvatar.AVATAR_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.ava);
        PlayerAvatar.STAR_BITMAPS = new Bitmap[] {
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_1),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_2),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_3),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_4),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_5)
        };
        PlayerAvatar.FLAG_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.flag);
        PlayerAvatar.READY_BITMAPS = new Bitmap[] {
    		BitmapFactory.decodeResource(getResources(), R.drawable.left_ready),
        	BitmapFactory.decodeResource(getResources(), R.drawable.right_ready),	
        };
        
        LinearLayout root = (LinearLayout) findViewById(R.id.root);
        
        final WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        final Display display = window.getDefaultDisplay();
        
        final DisplayMetrics metrics = new DisplayMetrics();
        
        display.getMetrics(metrics);
        
        
        ResolutionDetector.detect(this);
     
        final GameSurfaceView sharedDirectory = new GameSurfaceView(this) {
        	@Override
        	public void surfaceChanged(SurfaceHolder holder, int format,
        			int width, int height) {
        		Repository.SURFACE_WIDTH = width;
        		Repository.SURFACE_HEIGHT = height;
        		
        		
        		super.surfaceChanged(holder, format, width, height);
        	}
        };
        
        
        root.addView(sharedDirectory);
        
        final MainScene pMainScene = new MainScene();
        
        sharedDirectory.runWithScene(pMainScene);
        
        
        PlayerAvatar seat2 = new PlayerAvatar(2);
        seat2.setPosition(0, (Repository.SURFACE_HEIGHT - seat2.getHeight()) / 2);
        
        pMainScene.addChild(seat2);
        
        seat2.setPlayerName("MeoBeo2");
        seat2.setStar(5);
        
        
        PlayerAvatar seat3 = new PlayerAvatar(3);
        seat3.setPosition(Repository.SURFACE_WIDTH - seat3.getWidth(), (Repository.SURFACE_HEIGHT - seat3.getHeight()) / 2);
        
        seat3.setPlayerName("matdennoig");
        seat3.setStar(1);
        
        pMainScene.addChild(seat3);
        
        final Text mText = new Text("abcghpq");
        
        mText.setTextSize(40);
        mText.setPosition(0, Repository.SURFACE_HEIGHT - mText.getHeight());
        pMainScene.addChild(mText);
        
        mText.setAlpha(15);
        
        
        //pMainScene.addChild(new Card(mBitmap));
         */
        /*
        MainScene.BACKGROUND_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        
        mBitmapRed = BitmapFactory.decodeResource(getResources(), R.drawable.card_43);
        mBitmapBlack = BitmapFactory.decodeResource(getResources(), R.drawable.card_00);
        
        PlayerAvatar.AVATAR_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.ava);
        PlayerAvatar.STAR_BITMAPS = new Bitmap[] {
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_1),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_2),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_3),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_4),
        	BitmapFactory.decodeResource(getResources(), R.drawable.star_5)
        };
        PlayerAvatar.FLAG_BITMAP = BitmapFactory.decodeResource(getResources(), R.drawable.flag);
        PlayerAvatar.READY_BITMAPS = new Bitmap[] {
    		BitmapFactory.decodeResource(getResources(), R.drawable.left_ready),
        	BitmapFactory.decodeResource(getResources(), R.drawable.right_ready),	
        };
        
        final SurfaceView surfaceView = new SurfaceView(this);
        
        setContentView(surfaceView);
        
        final Engine engine = new Engine();
        
        surfaceView.getHolder().addCallback(engine);
        surfaceView.setOnTouchListener(engine);
        
        ResolutionDetector.detect(this);
        
        final MainScene pMainScene = new MainScene();
        
        //Background
        final Sprite background = new Sprite(MainScene.BACKGROUND_BITMAP);
        
        pMainScene.addChild(background);
        
        //Seat Bottom (Player)
        final PlayerAvatar seat0 = new PlayerAvatar(0);
        
        
        pMainScene.addChild(seat0);
        
        seat0.setPlayerName("huypq");
        seat0.setStar(5);
        seat0.setMoney(99999999);
        
        seat0.setFlag(true);
        
        //Seat Top
        final PlayerAvatar seat1 = new PlayerAvatar(1);
        
        
        pMainScene.addChild(seat1);
        
        seat1.setPlayerName("phanquanghuy");
        seat1.setStar(5);
        seat1.setMoney(100000);
        
        final PlayerAvatar seat2 = new PlayerAvatar(2);
        
        pMainScene.addChild(seat2);
        
        seat2.setPlayerName("MeoBeo2");
        seat2.setStar(5);
        
        
        final PlayerAvatar seat3 = new PlayerAvatar(3);
        
        
        seat3.setPlayerName("matdennoigi");
        seat3.setStar(5);
        
        pMainScene.addChild(seat3);
        
        final Text mText = new Text("abcghpq");
        
        mText.setTextSize(40);
        
        pMainScene.addChild(mText);
        
        mText.setAlpha(15);
        
        //Card
        final PlayerCardHolder holder = new PlayerCardHolder();
        
        pMainScene.addChild(holder);
       
        pMainScene.registerTouchArea(holder);
        
        for (int i = 0; i < 20; i++) {
        	if (i % 2 == 0) {
        		holder.addCard(mBitmapRed);
        	} else {
        		holder.addCard(mBitmapBlack);
        	}
        }
        
        final Sprite card = new Sprite(mBitmapRed);
        card.setSize((int)(50 * metrics.density), (int)(70 * metrics.density));
        pMainScene.addChild(card);
        
        pMainScene.registerTouchArea(seat0);
        */
        //Callback
        
        final Scene maineScene = new Scene();
        
        final Text label = new Text("Hello Canvas Engine 1.1");
        label.setColor(Color.WHITE);
        
        maineScene.addChild(label);
       
        final Text subLabel = new Text("Copyright Thudo JSC 2012");
        subLabel.setColor(Color.RED);
        
        label.addChild(subLabel);
        subLabel.setPosition(10.5f, 22.3f);
        
        final Sprite card = new Sprite(mBitmapRed);
        subLabel.addChild(card);
        
        final Sprite card1 = new Sprite(mBitmapBlack);
        card.addChild(card1);
        card1.setPosition(20, 20);
        
        engine.setEngineCallback(new Engine.IEngineCallback() {
			
			@Override
			public void onDrawAreaSizeChanged(int pAreaWidth, int pAreaHeight) {
				/*
				seat0.setPosition( (pAreaWidth - seat0.getWidth()) / 2  , pAreaHeight - seat0.getHeight());
				seat1.setPosition( (pAreaWidth - seat1.getWidth()) / 2  , 0);
				seat2.setPosition(0, (pAreaHeight - seat2.getHeight()) / 2);
				seat3.setPosition(pAreaWidth - seat3.getWidth(), (pAreaHeight - seat3.getHeight()) / 2);
				mText.setPosition(0, pAreaHeight - mText.getHeight());
				background.setSize(pAreaWidth, pAreaHeight);
				
				holder.setSize(pAreaWidth, Card.CARD_HEIGHT);
				holder.setPosition(0, pAreaHeight - Card.CARD_HEIGHT - 100);
				holder.onMeasure();
				*/
				
			}
			
			@Override
			public void onCompleted(Engine pEngine) {
				//engine.runWithScene(pMainScene);
				engine.runWithScene(maineScene);
			}
		});
    }
}